﻿Shader "Dan/UI/UI_Mosaic01"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _Density ("Density", Float ) = 8
        [MaterialToggle]_UseTime ("UseTime", Float ) = 0
        _Speed ("Speed", Float ) = 1
        _Emissin ("Emissin", Float ) = 1
        _Amount ("Amount", Range(0, 1)) = 0.7898054
        _MosaicStrength ("MosaicStrength", Float ) = 0
        _Pattern ("Pattern", 2D) = "white" {}
        
    }
    
    SubShader
    {

        Tags { "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

            float4 _Color;
            float _Density;
            float _Speed;
            float _Emissin;
            float _Amount;
            float _UseTime;
            sampler2D _Pattern; 
            float4 _Pattern_ST;
            float _MosaicStrength;

            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;        
                return o;
            }
            
            
            float Flick (float2 timeSpeed){
                float2 sk = timeSpeed + 0.2127+timeSpeed.x*0.3713*timeSpeed.y;
                float2 rnd = 4.789*sin(489.123*(sk));
                return frac(rnd.x*rnd.y*(1+sk.x));
            }

            //马赛克闪动
            float Mosaic (float2 uv, float density, float speed, float amount){                
                
                float2 rd= floor((uv+1) * density) / (density - 1)+speed;
                float flick = Flick(rd);
                float bg = saturate((1.1 - amount)*3.333333-2.333333);
                float mosaicRD =saturate(smoothstep( amount, 1.0, flick )+bg*bg);
                
                return mosaicRD;
            }    




            fixed4 frag (v2f i ) : SV_Target
            {
                
                
                float2 uv = i.uv ;
                
                float usetime =lerp( _Amount, _Time.y*_Speed, _UseTime )*0.0001;
                
                float mosaicRD =Mosaic(uv,_Density,usetime,_Amount);
                
                float3 finalColor = _Color.rgb*_Emissin;
                
                float mainTexAlpha = tex2D(_Pattern,TRANSFORM_TEX(uv, _Pattern)).a;


                return fixed4(finalColor,(mosaicRD*mainTexAlpha)+(mosaicRD*mainTexAlpha*_MosaicStrength));
           

                
         
         
                


            

            



            }
            ENDCG
        }
    }
    
    
    
}
